//package com.lebang.medicalInquiry.component;
//
//
//import org.quartz.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.quartz.QuartzJobBean;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import java.util.*;
//import java.util.concurrent.ThreadLocalRandom;
//
///**
// * @version V1.0
// * @author: zpc
// * @date:
// */
//@Service
//public class QuartzComponent {
//    @Autowired
//    private Scheduler scheduler;
//
//
//    @PostConstruct
//    public void startScheduler() {
//        try {
//            scheduler.start();
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//    public void getTrigger(Class<? extends QuartzJobBean> join, List<Date>  jobTime, Map jobData, String jobName, String jobGroupName, Map<JobDetail, Set<? extends Trigger>> triggersAndJobs){
//        JobDetail build = JobBuilder.newJob(join).withIdentity(jobName, jobGroupName).build();
//        if (jobData != null && jobData.size() > 0) {
//            build.getJobDataMap().putAll(jobData);
//        }
//        Set<Trigger> objects = new HashSet<>();
//        jobTime.forEach(s->{
//            int i = ThreadLocalRandom.current().nextInt(900000);
//            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName+System.currentTimeMillis()+i, jobGroupName+System.currentTimeMillis()+i).withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0))
//                    .forJob(build).startAt(s).build();
//            objects.add(trigger);
//        });
//        triggersAndJobs.put(build,objects);
//        try {
//            scheduler.scheduleJobs(triggersAndJobs,true);
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//    }
//
//    public boolean addJob(Class<? extends QuartzJobBean> join, String jobName, String jobGroupName, Date jobTime, Map jobData){
//        JobDetail build = JobBuilder.newJob(join).withIdentity(jobName, jobGroupName).build();
//        //给新建的任务指定
//        // 设置job参数
//        if (jobData != null && jobData.size() > 0) {
//            build.getJobDataMap().putAll(jobData);
//        }
//        /** 指定触发器规则
//         * @Author zpc
//         * @Date 12:29 2020/12/2
//         * @Param [join, jobName 工作名称, jobGroupName 工作租名称, jobTime 定时的时间 , jobData 传入的参数]
//         * startAt 代表当前任务在那个时间执行
//         * @return boolean
//         * withSchedule 指定两种时间调度器 SimpleScheduleBuilder(指定时间格式 date) 和 CronScheduleBuilder (指定时间格式 cron表达式)
//         * withRepeatCount 给触发器指定重复次数   forJob 将指定的JobDetail加入Trigger  startAt代表在什么时候开启
//         **/
//        Trigger build1 = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName).withSchedule(SimpleScheduleBuilder.simpleSchedule()
//                .withRepeatCount(0)).forJob(build).startAt(jobTime).build();
//        try {
//            //开启调度器
//                scheduler.scheduleJob(build,build1);
//            //启动所有定时任务
////            scheduler.start();
//            return true;
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
////批量插入job
//    public boolean addJobs(Map<JobDetail, Set<? extends Trigger>> map){
//        try {
//            scheduler.scheduleJobs(map,true);
//            //开启调度器
//
//            return true;
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//    public boolean updateJob(Class<? extends QuartzJobBean> join, String jobName, String jobGroupName, Date jobTime, Map jobData){
//        TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
//        try {
//            SimpleTrigger trigger = (SimpleTrigger)scheduler.getTrigger(triggerKey);
//            if (trigger!=null&&jobData != null && jobData.size() > 0) {
//                //重新赋值消息内容
//                trigger.getJobDataMap().putAll(jobData);
//                trigger=trigger;
//            }else{
//                addJob(join,jobName,jobGroupName,jobTime,jobData);
//                return true;
//            }
//            SimpleTrigger build = trigger.getTriggerBuilder().withIdentity(triggerKey).startAt(jobTime).withSchedule(SimpleScheduleBuilder.simpleSchedule().withRepeatCount(0)).build();
//            //重启调度器
//            scheduler.rescheduleJob(triggerKey,build);
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//            return false;
//        }
//        return true;
//    }
//    public boolean removeJob(String jobName, String jobGroupName){
//
//        try {
//
//            scheduler.deleteJob(new JobKey(jobName, jobGroupName));
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//            return false;
//        }
//        return true;
//    }
//}
